package com.woniuxy.article.service.impl;

import com.hc.entity.Comment;
import com.hc.entity.Paper;
import com.hc.vo.AgreeVo;
import com.woniuxy.article.mapper.CommentMapper;
import com.woniuxy.article.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description：
 * @Author：曹杰克
 * @CreateTime：2022/10/20 15:11
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Override
    public void addComment(Comment commnet) {
        commnet.setStatus("n");
        commnet.setUnlikenum(0);
        commnet.setLikenum(0);
        commentMapper.addCommentByPaperIdAndComentId(commnet);
    }

    @Override
    public void updateAgreenum(Comment commnet) {

    }

    /**
     * 踩
     * @param commnet
     */
    @Override
    public void updatedisAgreenum(Comment commnet) {
        //待续....并发问题
        int unlikeNum = commentMapper.getDisLikeNum(commnet.getId());
        commnet.setUnlikenum(unlikeNum+1);
        commentMapper.updateCommentdisAgreeByComentId(commnet);
    }

    /**
     * 评论置顶
     * @param commnet
     */
    @Override
    public void setTop(Comment commnet) {
        //现将相应的文章所用评论变为非置顶
        commentMapper.setdown(commnet.getPaper().getId());
        //将该评论变为置顶
        commentMapper.setTop(commnet.getId());
    }

    @Override
    public List<Comment> getByPaperId(Comment commnet) {
        List<Comment> byPaperId = commentMapper.getByPaperId(commnet.getPaper().getId());
        return byPaperId;
    }

    /**
     * 点赞
     * @param
     */
    @Override
    public boolean agree(AgreeVo agreeVo) {
        //1.获取当前用户
        int userId = agreeVo.getUserId();
        //2.判断是否已点赞
        String key ="评论"+agreeVo.getCommentId()+"已点赞";
        //根据是否可以存到redis中判断是否点赞
        Boolean member = stringRedisTemplate.opsForSet().isMember(key, userId + "");
        //没有被点赞
        if(!member){
            //评论点赞数加1
            commentMapper.addCommentAgreeByComentId(agreeVo.getCommentId());
            //将数据存到redis中
            stringRedisTemplate.opsForSet().add(key, userId + "");
            return true;
        }else {
            //被点赞，说明要取消点赞
            //评论点赞数-1
            boolean b = commentMapper.reCommentAgreeByComentId(agreeVo.getCommentId());
            //删除redis中信息
            if(b){
                Long set = stringRedisTemplate.opsForSet().size(key);
                Boolean me = stringRedisTemplate.opsForSet().isMember(key, userId + "");
                System.out.println(key);
                stringRedisTemplate.delete(key);
            }
            return false;
        }
    }

    @Override
    public Comment getTopComment(Comment commnet) {
        Comment commnet1=commentMapper.getTopComment(commnet.getPaper().getId());
        return commnet1;
    }

    @Override
    public void downtop(Comment commnet) {
        commentMapper.downTop(commnet.getId());
    }


}
